/* Base64 library.
 *
 * Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
 *               2006-2007 Ma Bingyao <andot@ujn.edu.cn>
 * Version: 1.28 $Id: Base64.js 106 2011-03-25 09:34:45Z dsonet@gmail.com $
 * LastModified: Mar 3, 2008
 * This library is free.  You can redistribute it and/or modify it.
 * for further information, see http://zh.wikipedia.org/wiki/Base64
 */
/*
 * Interfaces:
 * b64 = window.btoa(data);//b64 = Base64.encode(data);
 * data = window.atob(b64);data = Base64.decode(b64);
 */

if(typeof btoa != "function")
/**
 * Creates a base-64 encoded ASCII string from a string of binary data.
 * <code>var encodedData = window.btoa(stringToEncode);</code>
 * @param {string} stringToEncode a string of binary data
 * @return {string} base-64 encoded
 */
//only function define will cause error in Safari
var btoa = function(str) {
	var out, i = -1, j = -1, len = str.length, r = len % 3, l, c;
	len -= r;
	l = (len / 3) << 2;
	if (r > 0) {
		l += 4;
	}
	out = new Array(l);

	while (++i < len) {
		c = str.charCodeAt(i) << 16 |
			str.charCodeAt(++i) << 8  |
			str.charCodeAt(++i);
		out[++j] = base64EncodeChars[c >> 18]
			+ base64EncodeChars[c >> 12 & 0x3f]
			+ base64EncodeChars[c >> 6  & 0x3f]
			+ base64EncodeChars[c & 0x3f] ;
	}
	if (r == 1) {
		c = str.charCodeAt(i);
		out[++j] = base64EncodeChars[c >> 2]
			+ base64EncodeChars[(c & 0x03) << 4]
			+ "==";
		}
	else if (r == 2) {
		c = str.charCodeAt(i) << 8 |
			str.charCodeAt(++i);
		out[++j] = base64EncodeChars[c >> 10]
			 + base64EncodeChars[c >> 4 & 0x3f]
			 + base64EncodeChars[(c & 0x0f) << 2]
			 + "=";
	}
	return out.join('');
}

if(typeof atob != "function")
/**
 * Decodes a string of data which has been encoded using base-64 encoding.
 * <code>var decodedData = window.atob(encodedData);</code>
 * @param {string}  encodedData a string has been encoded using base-64 encoding
 * @return {string} Decoded string
 */
//only function define will cause error in Safari
var atob = function(str) {
	var c1, c2, c3, c4;
	var i, j, len = str.length, r, l, out;

	if (len % 4 != 0 || alphabet_inverse.test(str)) {
		return '';
	}
	r = str.charAt(len - 2) == '=' ? 1 : str.charAt(len - 1) == '=' ? 2 : 0;
	l = len;
	if (r > 0) {
		l -= 4;
	}
	l = (l >> 2) * 3 + r;
	out = new Array(l);

	i = j = -1;
	while (++i < len) {
		// c1
		c1 = base64DecodeChars[str.charCodeAt(i)];
		if (c1 == -1) break;

		// c2
		c2 = base64DecodeChars[str.charCodeAt(++i)];
		if (c2 == -1) break;

		out[++j] = (c1 << 2) | ((c2 & 0x30) >> 4);

		// c3
		c3 = base64DecodeChars[str.charCodeAt(++i)];
		if (c3 == -1) break;

		out[++j] = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);

		// c4
		c4 = base64DecodeChars[str.charCodeAt(++i)];
		if (c4 == -1) break;

		out[++j] = ((c3 & 0x03) << 6) | c4;
	}
	return String.fromCharCode.apply(String, out);
}
/**
 * base-64 encoding encode & decode.
 */
var Base64 = 
{
	/**
	 * Creates a base-64 encoded ASCII string from a string of binary data.
	 * @param {string} stringToEncode a string of binary data
	 * @id Base64.encode
	 * @return {string} base-64 encoded
	 */
	encode : btoa,
	/**
	 * Decodes a string of data which has been encoded using base-64 encoding.
	 * @param {string}  encodedData a string has been encoded using base-64 encoding
	 * @id Base64.decode
	 * @return {string} Decoded string
	 */
	decode : atob
}
, base64EncodeChars = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"]
, base64DecodeChars = [
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
	-1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
	-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
], alphabet_inverse = /[^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\+\/\=]/;
